package com.wisemoresystem.seal.action.user;

import org.ne.framework.data.util.ChkTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.base.tip.Tip;
import com.wisemoresystem.seal.model.t_user;
import com.wisemoresystem.seal.service.UserService;
import com.wisemoresystem.seal.util.md5.MD5;
import com.wisemoresystem.seal.web.SessionVal;

/**
 * 密码修改
 * 
 * @author Administrator
 *
 */
@Controller("ixUser")
@Scope("prototype")
@RequestMapping("/ix/user")
public class UserAction extends ProjBaseAction {

	@Autowired
	private UserService userService;

	@RequestMapping("/pwd_form")
	public ModelAndView pwd_form() {

		ModelAndView mv = new ModelAndView("/jsp/user/pwd_form");
		return mv;
	}

	/**
	 * 密码修改
	 * 
	 * @param oldpwd
	 *            旧密码
	 * @param password
	 *            新密码
	 * @param reppwd
	 *            新密码
	 * @return
	 */
	@RequestMapping("/pwd")
	public ModelAndView pwd(String oldpwd, String password, String reppwd) {
		if (ChkTools.isNull(password) && ChkTools.isNull(reppwd)) {
			super.setTipMsg("新密码不能为空", Tip.Type.error);
			ModelAndView mv = new ModelAndView("redirect:pwd_form.htm");
			return mv;
		}

		if (!password.equals(reppwd)) {
			super.setTipMsg("新密码不一致，请重新输入", Tip.Type.error);
			ModelAndView mv = new ModelAndView("redirect:pwd_form.htm");
			return mv;
		}

		t_user user = (t_user) req.getSession().getAttribute(
				SessionVal.login_user);

		t_user userDb = userService.selectUserByUnameAndPwd(user.getUname(),
				oldpwd);

		if (userDb == null) {
			super.setTipMsg("密码错误，修改不成功", Tip.Type.error);
			ModelAndView mv = new ModelAndView("redirect:pwd_form.htm");
			return mv;
		}

		userDb.setPwd(MD5.md5(password));
		oc.updateObj(userDb);
		req.getSession().setAttribute(SessionVal.login_user, userDb);

		logT.logH("修改密码", t_user.class, userDb.get_id());

		super.setTipMsg("修改成功!", Tip.Type.success);
		ModelAndView mv = new ModelAndView("/jsp/user/pwd_form");
		return mv;

	}
}
